package com.ruoyi.flowable.services.imp;

import com.ruoyi.flowable.services.ProcessDefinitonService;
import org.flowable.common.engine.api.FlowableException;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @program: RuoYi-Vue
 * @description: 流程定义service
 * @author: jhw
 * @create: 2021-04-13 09:28
 **/
@Service
public class ProcessDefinitionServiceImp implements ProcessDefinitonService {
    @Autowired
    protected RepositoryService repositoryService;
    @Override
    public ProcessDefinition queryProcessDefinition(String processDefinitionId, String key, String name) {
        return null;
    }

    @Override
    public void suspend(String definitionId, boolean isSuspendProcessInstances, Date suspendedDate) {
        ProcessDefinition processDefinition = repositoryService.getProcessDefinition(definitionId);
        if (processDefinition.isSuspended()) {
            throw new FlowableException("flowable.definition.suspendError" + definitionId);
        }
        repositoryService.suspendProcessDefinitionById(definitionId,isSuspendProcessInstances,suspendedDate);
    }

    @Override
    public void activate(String definitionId, boolean isSuspendProcessInstances, Date suspendedDate) {
        ProcessDefinition processDefinition = repositoryService.getProcessDefinition(definitionId);
        if (!processDefinition.isSuspended()) {
            throw new FlowableException("flowable.definition.activateError" + definitionId);
        }
        repositoryService.activateProcessDefinitionById(definitionId,isSuspendProcessInstances,suspendedDate);
    }

    @Override
    public void delete(String definitionId,boolean cascade) {
        ProcessDefinition definition = repositoryService.getProcessDefinition(definitionId);
        repositoryService.deleteDeployment(definition.getDeploymentId(),cascade);
    }
}
